<?php

/* @var $this \yii\web\View */
/* @var $content string */

use backend\assets\AppAsset;
use yii\helpers\Html;
use yii\bootstrap\Nav;
use yii\bootstrap\NavBar;
use yii\widgets\Breadcrumbs;
use common\widgets\Alert;
use yii\helpers\Url;
use izyue\admin\components\MenuHelper;

AppAsset::register($this);

$menuRows = MenuHelper::getAssignedMenu(Yii::$app->user->id);
$route = Yii::$app->controller->getRoute();
$routeArray = explode('/', $route);
array_pop($routeArray);
$controllerName = implode('/', $routeArray);

$this->registerCssFile('@web/statics/css/slidebars.css', ['depends'=>'backend\assets\AppAsset']);

function isSubUrl($menuArray, $route)
{

    if (isset($menuArray) && is_array($menuArray)) {

        if (isset($menuArray['items'])) {
            foreach ($menuArray['items'] as $item)
            {
                if (isSubUrl($item, $route)) {
                    return true;
                }
            }
        } else {
            $url = is_array($menuArray['url']) ? $menuArray['url'][0] : $menuArray['url'];
            if (strpos($url, $route)) {
                return true;
            }
        }
    } else {
        $url = is_array($menuArray['url']) ? $menuArray['url'][0] : $menuArray['url'];
        if (strpos($url, $route)) {
            return true;
        }
    }
    return false;

}

function isSubMenu($menuArray, $controllerName)
{

    if (isset($menuArray) && is_array($menuArray)) {

        if (isset($menuArray['items'])) {
            foreach ($menuArray['items'] as $item)
            {
                if (isSubMenu($item, $controllerName)) {
                    return true;
                }
            }
        } else {
            $url = is_array($menuArray['url']) ? $menuArray['url'][0] : $menuArray['url'];
            if (strpos($url, $controllerName.'/')) {
                return true;
            }
        }
    } else {
        $url = is_array($menuArray['url']) ? $menuArray['url'][0] : $menuArray['url'];
        if (strpos($url, $controllerName.'/')) {
            return true;
        }
    }
    return false;

}



function initMenu($menuArray, $controllerName, $isSubUrl, $isShowIcon=false)
{
    if (isset($menuArray) && is_array($menuArray)) {

        $url = is_array($menuArray['url']) ? $menuArray['url'][0] : $menuArray['url'];

        if (empty($isSubUrl)) {
            $isSubMenu = isSubMenu($menuArray, $controllerName);
        } else {
            $route = Yii::$app->controller->getRoute();
            $isSubMenu = isSubUrl($menuArray, $route);
        }
        if ($isSubMenu) {
            $class = ' active ';
        } else {
            $class = '';
        }

        if (isset($menuArray['items'])) {
            echo '<li class="sub-menu">';
        } else {
            echo '<li class="'.$class.'">';
        }
        $url = $url == '#' ? 'javascript:;' : Url::toRoute($url);
        echo '<a href="'.$url.'"  class="'.$class.'">'.($isShowIcon ? '<i class="fa fa-sitemap"></i>' : '').'<span>'.$menuArray['label'].'</span></a>';

        if (isset($menuArray['items'])) {
            echo '<ul class="sub">';
            foreach ($menuArray['items'] as $item)
            {
                echo initMenu($item, $controllerName, $isSubUrl);
            }
            echo '</ul>';
        }

        echo '</li>';
    }

}

?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta  http-equiv="Cache-Control"   CONTENT="private,must-revalidate">
    <?= Html::csrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>

<section id="container" >
    <!--header start-->
    <header class="header white-bg">
        <div class="sidebar-toggle-box">
            <div class="fa fa-bars tooltips" data-placement="right" data-original-title="Toggle Navigation"></div>
        </div>
        <!--logo start-->
        <a href="<?=Url::home()?>" class="logo"><span>养殖溯源农业管理系统</span></a>
        <!--logo end-->
        <div class="nav notify-row" id="top_menu">
            <!--  notification start -->
            <!--  notification end -->
        </div>
        <div class="top-nav ">
            <!--search & user info start-->
            <ul class="nav pull-right top-menu">
                <li>
                    <input type="text" class="form-control search" name="search" placeholder="输入菜单名称快速检索">
                    <div class="searchBox" display: none"></div>
                </li>
                <!-- user login dropdown start-->
                <li class="dropdown">
                    <a data-toggle="dropdown" class="dropdown-toggle" href="#">
                        <img alt="" src="<?=Yii::getAlias('@web')?>/statics/img/avatar1_small.jpg">
                        <span class="username"><?=Yii::$app->user->identity['username']?></span>
                        <b class="caret"></b>
                    </a>
                    <ul class="dropdown-menu extended logout">
                        <div class="log-arrow-up"></div>
<!--                        <li><a href="#"><i class=" fa fa-suitcase"></i>Profile</a></li>-->
<!--                        <li><a href="#"><i class="fa fa-cog"></i> Settings</a></li>-->
<!--                        <li><a href="#"><i class="fa fa-bell-o"></i> Notification</a></li>-->
                        <li><a href="<?=Url::toRoute('/site/logout')?>" data-method="post"><i class="fa fa-key"></i>退出登录</a></li>
                    </ul>
                </li>
<!--                <li class="sb-toggle-right">-->
<!--                    <i class="fa  fa-align-right"></i>-->
<!--                </li>-->
                <!-- user login dropdown end -->
            </ul>
            <!--search & user info end-->
        </div>
    </header>
    <!--header end-->
    <!--sidebar start-->
    <aside>
        <div id="sidebar"  class="nav-collapse ">
            <!-- sidebar menu start-->
            <ul class="sidebar-menu" id="nav-accordion">
                <li>
                    <a class="<?=($controllerName == 'site' ? 'active' : '')?>" href="<?=Url::home()?>">
                        <i class="fa fa-dashboard"></i>
                        <span><?=Yii::t('admin', 'dashboard')?></span>
                    </a>
                </li>
                <?php
                    if(isset($menuRows)){

                        $isSubUrl = false;
                        foreach($menuRows as $menuRow){

                            $isSubUrl = isSubUrl($menuRow, $route);

                            if ($isSubUrl) {
                                break;
                            }


                        }
                        foreach($menuRows as $menuRow){

                            initMenu($menuRow, $controllerName, $isSubUrl, true);
                        }
                    }
                ?>

            </ul>
            <!-- sidebar menu end-->
        </div>
    </aside>
    <!--sidebar end-->
    <!--main content start-->
    <section id="main-content">
        <?=$content?>
    </section>
    <!--main content end-->

    <!-- Right Slidebar start -->
    <!-- Right Slidebar end -->

    <!--footer start-->
    <footer class="site-footer">
        <div class="text-center">
            2017 &copy; <a href="http://www.wayfiretech.com" style="color:#fff" target="_blank">途火科技</a>（www.wayfiretech.com）
            <a href="#" class="go-top">
                <i class="fa fa-angle-up"></i>
            </a>
        </div>
    </footer>
    <!--footer end-->
</section>


<script>

    <?php $this->beginBlock('js_end') ?>

    //搜索菜单
    $(document).ready(function(){
        $('input[name="search"]').keyup(function() {
            shishiSearch(this)
        });
        //实时检索
        function shishiSearch(obj)
        {
            var xhr=null;
            if(xhr){
                xhr.abort();//如果存在ajax的请求，就放弃请求
            }
            var inputText= $.trim(obj.value);
            var kwd = $(obj)
            if(inputText!=""){//检测键盘输入的内容是否为空，为空就不发出请求
                xhr=$.ajax({
                    type: 'GET',
                    url: '/business/search/search-menu',
                    cache:false,//不从浏览器缓存中加载请求信息
                    data: "q=" + inputText,//向服务器端发送的数据
                    dataType: 'json',//服务器返回数据的类型为json
                    success: function (json) {
                        if (json.results.length != 0) {//检测返回的结果是否为空
                            var lists = "<ul style='width:400px;'>";
                            $.each(json.results, function(index,element) {
                                if(element.route != null){
                                    lists += "<li class='liclick' data-id="+element.id+"  data-route='"+element.route+"'>"+element.text+"</li>";//遍历出每一条返回的数据
                                }
                            });
                            lists+="</ul>";

                            kwd.siblings('.searchBox').html(lists).show();//将搜索到的结果展示出来

                            $(".liclick").click(function(){
                                window.location.href = $(this).data('route')
                            })

                        }
                    }

                });
            }else{
                kwd.siblings('.searchBox').hide();//没有查询结果就隐藏搜索框
            }

            kwd.siblings('.searchBox').blur(function(){
                kwd.siblings('.searchBox').hide();//输入框失去焦点的时候就隐藏搜索框
            })
        }
    });
    <?php $this->endBlock(); ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>

</script>


<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
